package com.ruiyian.bridge.info.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ruiyian.bridge.info.domain.DistanceInfo;
import com.ruiyian.bridge.info.mapper.DistanceInfoMapper;
import com.ruiyian.bridge.info.service.DistanceInfoService;
import com.ruiyian.bridge.project.domain.vo.*;
import com.ruiyian.common.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2022/6/15
 * Desc: 激光传感器
 */
@Service
public class DistanceInfoServiceImpl extends ServiceImpl<DistanceInfoMapper, DistanceInfo> implements DistanceInfoService {

    @Autowired
    private DistanceInfoMapper distanceInfoMapper;


    @Override
    public DataAnalysisVo getNowMinData(String addr, String macaddr) {
        String now = DateUtils.dateTimeNow();
        String nextDate = DateUtils.getNextDate(2, new Date(), -8, null);
        return distanceInfoMapper.getNowMinData(addr,macaddr,nextDate,now);
    }
    /**
     * 获取激光传感器数据统计
     * @param bo
     * @return
     */
    @Override
    public Map<String, Object> queryDataListByAddr(Mmh2oQueryBo bo) {
        Map<String,Object> retrurnData = Maps.newHashMap();
        List<String> xAxis = Lists.newArrayList();
        List<String> legend = Lists.newArrayList();
        List<Double> ydata = Lists.newArrayList();
        List<Map<String,Object>> series = Lists.newArrayList();
        if(StringUtils.isNotBlank(bo.getSearchTime())){
            bo.setStartTime(bo.getSearchTime()+" 00:00:00");
            bo.setEndTime(bo.getSearchTime()+" 23:59:59");
        }
        List<DistanceInfo> list = distanceInfoMapper.queryDataListByAddr(bo);
        list.forEach(detail -> {
            xAxis.add(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm",detail.getCreattime()));
            ydata.add(Double.parseDouble(detail.getDistance()));
        });
        Map<String,Object> data = new HashMap<>();
        data.put("data",ydata);
        data.put("name","距离");
        series.add(data);
        legend.add("距离");
        retrurnData.put("legend",legend);
        retrurnData.put("category",xAxis);
        retrurnData.put("series",series);
        return retrurnData;
    }
    /**
     * 多个类型统计 原始,10min,小时,日
     * @param dataAnalysisDto
     * @return
     */
    @Override
    public List<DataAnalysisVo> queryDisByDateType(DataAnalysisDto dataAnalysisDto) {
        return distanceInfoMapper.queryDisByDateType(dataAnalysisDto);
    }

    /**
     * 获取未同步的数据
     * @param startNum
     * @param endNum
     * @return
     */
    @DS("bsalve")
    @Override
    public List<DataAnalysisVo> getDataList(Long startNum, Long endNum) {
        return distanceInfoMapper.getDataList(startNum, endNum);
    }
    /**
     * 本地数据个数
     * @return
     */
    @Override
    public Long queryCount() {
        return distanceInfoMapper.queryCount();
    }
    /**
     * 本地数据个数
     * @return
     */
    @DS("bsalve")
    @Override
    public Long queryRemoteCount() {
        return distanceInfoMapper.queryRemoteCount();
    }

    @Override
    public List<DataAnalysisVo> getNowMinDataByType(DataAnalysisDto dataAnalysisDto) {
        return distanceInfoMapper.getNowMinDataByType(dataAnalysisDto);
    }

    @Override
    public DataAnalysisVo queryNowDisByDateType(DataAnalysisDto dataAnalysisDto) {
        return distanceInfoMapper.queryNowDisByDateType(dataAnalysisDto);
    }
    /**
     * 桥梁分组统计激光位移
     * @param bo
     * @return
     */
    @Override
    public List<BridgeSummaryVo> queryPageListByBridgeList(Mmh2oDayQueryBo bo) {
        if (bo.getDateType() == null && org.springframework.util.StringUtils.isEmpty(bo.getStartTime())){
            bo.setDateType(3);
        }

        if (bo.getDateType() != null){
            bo.setStartTime(DateUtils.getBeginOfDate(bo.getDateType()));
            bo.setEndTime(DateUtils.datetimeFormat.get().format(new Date()));
        } else if ( !org.springframework.util.StringUtils.isEmpty(bo.getStartTime())){
            bo.setStartTime(bo.getStartTime() + " 00:00:00");
            bo.setEndTime(bo.getEndTime() + " 23:59:59");
        }
        return distanceInfoMapper.queryPageListByBridgeList(bo);
    }

    /**
     * 根据桥墩分组统计
     * @param bo
     * @return
     */
    @Override
    public List<BridgeSummaryVo> queryPageListByPier(Mmh2oDayQueryBo bo) {
        if (bo.getDateType() == null && org.springframework.util.StringUtils.isEmpty(bo.getStartTime())){
            bo.setDateType(3);
        }

        if (bo.getDateType() != null){
            bo.setStartTime(DateUtils.getBeginOfDate(bo.getDateType()));
            bo.setEndTime(DateUtils.datetimeFormat.get().format(new Date()));
        } else if ( !org.springframework.util.StringUtils.isEmpty(bo.getStartTime())){
            bo.setStartTime(bo.getStartTime() + " 00:00:00");
            bo.setEndTime(bo.getEndTime() + " 23:59:59");
        }

        return distanceInfoMapper.queryPageListByPier(bo);
    }
}
